/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ListFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 *
 * @author ash
 */

public class ListFile {
    
    static Logger log = Logger.getLogger(ListFile.class);
    
    private final String defaultPath="c:\\BuyList.txt";
    private final String propFile="override.properties";
    private final String propNameLocation="location";
    private String output;
    
    private void initLogger(){
        //in case if logger is no initted under webserver - perform basic initialization.
        if (!log.getAllAppenders().hasMoreElements()){    
            try{
            BasicConfigurator.resetConfiguration();
            BasicConfigurator.configure(new FileAppender(new PatternLayout("%-4r %-5p %c{2} %M.%L %x - %m\n"), "BuyListDefaultLog.log"));
            log.setLevel(Level.INFO);
            } catch (IOException e){
                System.out.println(e.getMessage());
            }
        }
    }
    
    public ListFile(){   
        initLogger();
        log.info("Buy List application started.");
        output=getRecords(getPath());
    }
    
    public String print(){
        if (output == null){
            log.info("Error message printed.");
            return "Error. See log for details.";
        }
        
        log.info("List printed.");
        return output;
    }
    
    private String getPath(){
        String path=defaultPath;
        Properties p = new Properties();
        
        InputStream is = getClass().getResourceAsStream(propFile);
        try {
            p.load(is);
            log.debug("Loading resource [" + propFile + "].");
            is.close();
            if (p.containsKey(propNameLocation)){
                    log.debug("[" + propNameLocation + "] property present in resource. Its value will be used.");
                path = p.getProperty(propNameLocation).trim();
            } else {
                    log.debug("[" + propNameLocation + "] property is not found in resource. Default value will be used.");
            }
        } catch (IOException e){
                log.debug(e.getMessage());
        }
        
            log.info("Will read from file [" + path + "].");
        
        return path;
    }
    
    private String getRecords(String path){
        String str = null;
        int lineCount = 0;
        
        try {
            Scanner records;
            records = new Scanner(new File(path));
            str = "";
            
            while (records.hasNext()) {
                str = str + format(records.nextLine());
                lineCount++;
            }
            log.info(lineCount + " lines processed. File processing completed.");
        } catch (FileNotFoundException e){
                log.error(e.getMessage());
        }   
        return str;
    }

    private String format(String line){
            log.debug("New line: [" + line + "].");
        return "<p>" + line + "</p>\n";
    }
    
}
